package StackAndQueueSeries;

import java.util.ArrayDeque;
import java.util.Deque;

public class RemoveDuplicatesSolution_1047 {
    public String removeDuplicates(String s) {
        // 设计栈，入栈时判断与栈顶元素是否相同
        Deque<Character> deque = new ArrayDeque<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (!deque.isEmpty() && deque.peek() == ch) {
                deque.pop();
            } else {
                deque.push(ch);
            }
        }
        String res = "";
        while (!deque.isEmpty()) {
            res = deque.pop() + res;
        }
        return res;
    }
}
